<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/7/29
 * Time: 11:33
 */

namespace app\admin\controller;


use think\Request;

class Cate extends Base
{
    /**
     * 分类列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $where['cate_status'] = ['between', [1, 2]];
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = db('cate')->where($where)->count();// 获取总条数
            $lists = db('cate')
                ->field('cate_id, cate_name, cate_image, cate_status, cate_time')
                ->where($where)
                ->order('cate_time desc')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加分类
     * @return mixed|\think\response\Json
     */
    public function addCate(){
        if(Request::instance()->isPost()){
            $cate_name = trim(input('cate_name'));
            $cate_image = input('cate_image');
            $cate_status = input('cate_status');
            
            $insert = [
                'cate_name' => $cate_name,
                'cate_status' => $cate_status,
                'cate_image' => $cate_image,
                'cate_time' => date('Y-m-d H:i:s')
            ];
            $res = db('cate')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return view();
    }
    
    /**
     * 编辑分类
     * @return \think\response\Json|\think\response\View
     */
    public function editCate(){
        $cate_id = input('cate_id');
        if(Request::instance()->isPost()){
            $cate_name = trim(input('cate_name'));
            $cate_image = input('cate_image');
            $cate_status = input('cate_status');
            
            $update = [
                'cate_id' => $cate_id,
                'cate_name' => $cate_name,
                'cate_status' => $cate_status,
                'cate_updatetime' => date('Y-m-d H:i:s')
            ];
            if($cate_image) $update['cate_image'] = $cate_image;
            $res = db('cate')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $cate = db('cate')->where('cate_id', $cate_id)->field('cate_image, cate_name, cate_status')->find();
        $this->assign([
            'cate_id' => $cate_id,
            'cate' => $cate
        ]);
        return view();
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function cateDown(){
        $cate_id = input('id');
        $res = db('cate')->where('cate_id', $cate_id)->setField('cate_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function cateUp(){
        $cate_id = input('id');
        $res = db('cate')->where('cate_id', $cate_id)->setField('cate_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function delCate(){
        $cate_id = input('id');
        $res = db('cate')->where('cate_id', $cate_id)->setField('cate_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}